export const InterceptLabel = (name, max) => {
	let _name = name;
	let j = 0;
	for(var i = 0; i <= _name.length - 1; i++) {
		j = j + 1;
		if((_name.charCodeAt(i)) > 127) {
			j = j + 1;
		}
		if(j > max) {
			return _name.substring(0, i) + '...';
		}
	}
	return _name;
}

export const echartTipNewLine = (title ='', length) => {
    let _title = title;
    let j = 0;
    let r = [];
    let start  = 0;
    for(var i = 0; i <= _title.length - 1; i++) {
        j = j + 1;
        if((_title.charCodeAt(i)) > 127) {
            j = j + 1;
        }
        if(j > length) {
            r.push(_title.substring(start, i));
			j = 0;
			start = i;
        }
    }
    if(j > 0) {
        r.push(_title.substring(start));
	}
    if(r.length == 0) {
    	r.push(title);
	}
    return r.join('<br/>');
}

export const getEchartTipInfo = (params = []) =>{
	if(params.length == 1) {
        let tipInfo  = [];
        const {axisValue = '',marker,name,seriesName,data = {}} = params[0];
        const {value} = data;
        tipInfo.push(marker);
        tipInfo.push(echartTipNewLine(axisValue, 50));
        tipInfo.push('：'+value);
        return tipInfo.join('');
	}
}

export const downloadCvs2Img = (cvs,picName = 'Download') => {
	let $a = document.createElement('a');
	$a.download = `${picName}.jpg`;
	$a.target = '_blank';
	const url = cvs.toDataURL("image/jpg");
	$a.href = url;
	if(typeof MouseEvent === 'function') {
		var evt = new MouseEvent('click', {
			view: window,
			bubbles: true,
			cancelable: false
		});
		$a.dispatchEvent(evt);
	} else {
		if(window.navigator.msSaveOrOpenBlob) {
			var bstr = atob(url.split(',')[1]);
			var n = bstr.length;
			var u8arr = new Uint8Array(n);
			while(n--) {
				u8arr[n] = bstr.charCodeAt(n);
			}
			var blob = new Blob([u8arr]);
			window.navigator.msSaveOrOpenBlob(blob, 'Download.jpg');
		} else {
			// var lang = model.get('lang');
			var html = '' +
				'<body style="margin:0;">' +
				'<img src="' + url + '" style="max-width:100%;" title="" />' +
				'</body>';
			var tab = window.open();
			tab.document.write(html);
		}
	}
}

/**
 * 获取浏览框默认值
 */
export const getDefaultId = (keyId = '', selectLinkageDatas = {}) => {
	const brow = selectLinkageDatas[keyId + ''] || {};
	const {
		browserConditionParam = {}
	} = brow;
	const {
		replaceDatas = []
	} = browserConditionParam;
	if(replaceDatas.length > 0) {
		return replaceDatas[0].id || '';
	}
	return '';
}